package com.iseastar.guojiang.wallet;

import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.Gravity;
import android.view.View;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.WalletMoneyRecordBean;
import com.shouhuobao.station.R;

import droid.frame.activity.title.TitleRes;
import droid.frame.ui.utils.Utils;
import droid.frame.view.xlist.FooterListener;
import droid.frame.view.xlist.SListView;

/**
 * 火箭侠资金记录
 * Created by lidandan on 2018/1/16.
 */
public class WalletMoneyRecordActivity extends BaseActivity2 {

    private WalletMoneyRecordAdapter mAdapter;
    private ReqResult<WalletMoneyRecordBean> result = null;
    private SListView mListView;

    private int type;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadData(1, 0);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.wallet_money_record);
        super.findViewById();
        getAppTitle().setCommonTitle("资金记录", new TitleRes("筛选", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WalletMoneyFilterPopwin popwin = new WalletMoneyFilterPopwin(WalletMoneyRecordActivity.this, type);
                View rightView = findViewById(R.id.title_right_text);
                int[] location = new int[2];
                rightView.getLocationOnScreen(location);
                int x = (int) (16 * getResources().getDisplayMetrics().density);
                int y = location[1] + rightView.getHeight() + Utils.dpToPx(10, getResources());
                popwin.showAtLocation(rightView, Gravity.RIGHT | Gravity.TOP, x, y);
            }
        }));
        mListView = (SListView) findViewById(R.id.listView);
        mAdapter = new WalletMoneyRecordAdapter(null, getContext());
        mListView.setAdapter(mAdapter);

        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //如果有条件的话，就按条件来刷新，负责按照全部刷新
                AppHttp.getInstance().walletMoneyRecord(1, type);
            }
        });
        mListView.setFooterListener(new FooterListener() {
            @Override
            public void onFooterVisible() {
                //如果有条件的话，就按条件来请求，负责按照全部请求
                if (result != null && result.getPageNum() < result.getTotalPage()) {
                    AppHttp.getInstance().walletMoneyRecord(result.getPageNum() + 1, type);
                }
            }
        });

    }

    /**
     * 请求数据
     *
     * @param pageNo
     * @param type
     */
    public void loadData(int pageNo, int type) {
        this.type = type;
        showLoadingDialog(null);
        AppHttp.getInstance().walletMoneyRecord(pageNo, type);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.wallet_money_record:
                cancelLoadingDialog();
                onRefreshComplete();
                result = JSON.parseList(msg.obj, WalletMoneyRecordBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (checkLoginStatus(result)) {
                            if (mAdapter == null) {
                                mAdapter = new WalletMoneyRecordAdapter(null, getContext());
                                mListView.setAdapter(mAdapter);
                            }
                            if (result.getPageNum() == 1) {
                                mAdapter.setItems(result.getResultList(), true);
                            } else {
                                mAdapter.appendItems(result.getResultList(), true);
                            }
                            mListView.updateFooter(result.getPageNum(), result.getTotalPage());
                        } else {
                            showToast(result.getMessage());
                        }
                    }
                });
                return true;
            default:
                return super.handleMessage(msg);
        }
    }
}
